package com.zzyl.nursing.service.impl;


import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.mapper.NursingElderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-25
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {

        return getById(id);
    }

    @Autowired
    private NursingElderMapper nursingElderMapper; // 护理员-老人关联关系的Mapper

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData) {
        LambdaQueryWrapper<AlertData> queryWrapper = new LambdaQueryWrapper<>();

        // 1. 业务条件：设备名、ID、状态、时间范围
        if (alertData.getDeviceName() != null && !alertData.getDeviceName().isEmpty()) {
            queryWrapper.eq(AlertData::getDeviceName, alertData.getDeviceName());
        }
        if (alertData.getId() != null) {
            queryWrapper.eq(AlertData::getId, alertData.getId());
        }
        if (alertData.getStatus() != null) {
            queryWrapper.eq(AlertData::getStatus, alertData.getStatus());
        }
        if (alertData.getCreateTime() != null && alertData.getProcessingTime() != null) {
            queryWrapper.between(AlertData::getCreateTime, alertData.getCreateTime(), alertData.getProcessingTime());
        }

        // 2. 用户权限条件  直接让数据库过滤当前用户ID
        Long currentUserId = SecurityUtils.getUserId();
        queryWrapper.eq(AlertData::getUserId, currentUserId); // 核心优化：数据库层过滤用户

        // 3. 直接查询 业务条件 + 用户权限 匹配的数据
        return alertDataMapper.selectList(queryWrapper);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }

}
